package com.handpay.zztong.hp.swiper.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.handpay.framework.swiper.SwiperModel;
import com.handpay.framework.utils.CommonUtils;
import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.adapter.SwiperModelAdapter;
import com.handpay.zztong.hp.config.ZZTConstant;

public class SwiperSelectView extends LinearLayout implements OnClickListener {
    private ListView voice_mDeviceType;
    private ListView bluetooth_mDeviceType;
    private ScrollView sv;
    private TextView swiper_tips_tv;
    private OnSwiperSelectOnClickListenter onSelectClick;
    private Context mContext;
    private LinearLayout tabs_llo1 = null;
    private RelativeLayout linear_parent = null;
    private TextView tabs_iv1, tabs_hint1 = null;
    /**
     * 选项卡 1表示音频刷卡器类型 2表示蓝牙刷卡器类型
     */
    private int selectDiviceType = 1;
    private String[] voice_devices = null;
    private int[] voice_models = null;
    private int voice_icon = R.array.voice_swiper_icon;

    private String[] bluetooth_devices = null;
    private int[] bluetooth_models = null;
    private int bluetooth_icon = R.array.bluetooth_swiper_icon;
    private View v = null;

    public void setOnSelectClick(OnSwiperSelectOnClickListenter onSelectClick) {
        this.onSelectClick = onSelectClick;
    }

    public interface OnSwiperSelectOnClickListenter {
        void onSecletClick(String mDeviceName, int[] models, SwiperModel swiperModel, int position);
    }

    /**
     * @param context
     * @param selectDiviceType 选项卡初始化默认选项
     */
    public SwiperSelectView(Context context, int selectDiviceType) {
        super(context);
        this.selectDiviceType = selectDiviceType;
        mContext = context;
        v = LayoutInflater.from(context).inflate(R.layout.layout_swiper_select, null);
        initView(v);
        SwiperSelectView.this.addView(v, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    }

    private void initView(View v) {
        sv = (ScrollView) v.findViewById(R.id.swiper_scrollView);
        swiper_tips_tv = (TextView) v.findViewById(R.id.swiper_tips_tv);
        tabs_llo1 = (LinearLayout) v.findViewById(R.id.tabs_llo1);
        tabs_llo1.setBackgroundResource(selectDiviceType == 1 ? R.drawable.tab_two : R.drawable.tab_one);
        linear_parent = (RelativeLayout) v.findViewById(R.id.listview_parent);
        tabs_iv1 = (TextView) v.findViewById(R.id.tabs_voice);
        tabs_hint1 = (TextView) v.findViewById(R.id.tabs_bluetooth);
        tabs_iv1.setOnClickListener(this);
        tabs_hint1.setOnClickListener(this);
        tabs_llo1.setOnClickListener(this);
//		if (selectDiviceType == 1) {
//			initVoiceDeveice();
//		} else if (selectDiviceType == 2) {
//			initBluetoothDevice();
//		}
        this.postDelayed(new Runnable() {
            @Override
            public void run() {
                initVoiceDeveice();
            }
        }, selectDiviceType == 1 ? 0 : 10);
        this.postDelayed(new Runnable() {
            @Override
            public void run() {
                initBluetoothDevice();
            }
        }, selectDiviceType == 2 ? 0 : 10);

        swiper_tips_tv.setText(selectDiviceType == 1 ? R.string.connect_swipe_tips_voice : R.string.connect_swipe_tips_bluetooth);
        v.findViewById(R.id.nextButton).setOnClickListener(CommonUtils.createNoQuickResponseViewListener(ZZTConstant.NO_RESPONSE_CLICK_TIME, new OnClickListener() {
            @Override
            public void onClick(View v) {
                int i = -1;
                i = selectDiviceType == 1 ? voice_mDeviceType.getCheckedItemPosition() : bluetooth_mDeviceType.getCheckedItemPosition();
                if (-1 == i) {
                    Toast.makeText(mContext, R.string.please_select_swiper, Toast.LENGTH_SHORT).show();
                    return;
                }
                SwiperModel swiperModel = SwiperModel.values()[selectDiviceType == 1 ? voice_models[i] : bluetooth_models[i]];
                if (null != onSelectClick) {
                    onSelectClick.onSecletClick(selectDiviceType == 1 ? voice_devices[i] : bluetooth_devices[i], selectDiviceType == 1 ? voice_models : bluetooth_models, swiperModel, i);
                }
            }
        }));
        sv.smoothScrollTo(0, 0);
    }

    /**
     * 初始化音频刷卡器列表
     */
    private void initVoiceDeveice() {
        // swiper_tips_tv.setText(this.getResources().getString(R.string.connect_swipe_tips_voice));
        voice_devices = getSwiperType(R.array.voice_swiper_type);
        // 将选择的刷卡器，与SwipeModel对应
        voice_models = getSwiperModle(R.array.voice_swiper_model);
        voice_icon = R.array.voice_swiper_icon;
        voice_mDeviceType = createListView();
        SwiperModelAdapter voice_adapter = new SwiperModelAdapter(mContext, voice_devices, getDrawables(voice_icon));
        voice_mDeviceType.setAdapter(voice_adapter);
        setListViewHeightBasedOnChildren(voice_mDeviceType);
        linear_parent.addView(voice_mDeviceType);
        voice_mDeviceType.setVisibility(selectDiviceType == 1 ? View.VISIBLE : View.GONE);
    }

    private void initBluetoothDevice() {
        bluetooth_devices = getSwiperType(R.array.bluetooth_swiper_type);
        bluetooth_models = getSwiperModle(R.array.bluetooth_swiper_model);
        bluetooth_icon = R.array.bluetooth_swiper_icon;
        bluetooth_mDeviceType = createListView();
        SwiperModelAdapter bluetooth_adapter = new SwiperModelAdapter(mContext, bluetooth_devices,getDrawables(bluetooth_icon) );
        bluetooth_mDeviceType.setAdapter(bluetooth_adapter);
        setListViewHeightBasedOnChildren(bluetooth_mDeviceType);
        linear_parent.addView(bluetooth_mDeviceType);
        bluetooth_mDeviceType.setVisibility(selectDiviceType == 2 ? View.VISIBLE : View.GONE);
    }

    private void changedata() {
        if (selectDiviceType == 1) {
            bluetooth_mDeviceType.setVisibility(View.GONE);
            voice_mDeviceType.setVisibility(View.VISIBLE);
        } else if (selectDiviceType == 2) {
            voice_mDeviceType.setVisibility(View.GONE);
            bluetooth_mDeviceType.setVisibility(View.VISIBLE);
        }
        swiper_tips_tv.setText(selectDiviceType == 1 ? R.string.connect_swipe_tips_voice : R.string.connect_swipe_tips_bluetooth);
    }

    /**
     * 处理分栏点击切换 如果需要加其他的点击事件需要将changedata提前
     */
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tabs_voice) {
            selectDiviceType = 1;
            tabs_llo1.setBackgroundResource(R.drawable.tab_two);
        } else if (v.getId() == R.id.tabs_bluetooth) {
            selectDiviceType = 2;
            tabs_llo1.setBackgroundResource(R.drawable.tab_one);
        }
        changedata();
    }

    public void setListViewHeightBasedOnChildren(ListView listView) {
        // 获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0); // 计算子项View 的宽高
            totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        // listView.getDividerHeight()获取子项间分隔符占用的高度
        // params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }

    public int getSelectDiviceType() {
        return selectDiviceType;
    }

    public void setSelectDiviceType(int selectDiviceType) {
        this.selectDiviceType = selectDiviceType;
    }

    private ListView createListView() {
        ListView listview = new ListView(mContext);
        listview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        listview.setBackgroundColor(Color.WHITE);
        listview.setCacheColorHint(Color.BLACK);
        listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        listview.setSelector(android.R.color.transparent);
        return listview;
    }

    private String[] getSwiperType(int resid) {
        return getResources().getStringArray(resid);
    }

    private int[] getSwiperModle(int resid) {
        return getResources().getIntArray(resid);
    }

    private Drawable[] getDrawables(int resid) {
        TypedArray typedArray = getResources().obtainTypedArray(resid);
        Drawable[] drawables = new Drawable[typedArray.length()];
        for (int i = 0; i < typedArray.length(); i++) {
            drawables[i] = this.getResources().getDrawable(typedArray.getResourceId(i, 0));
        }
        return drawables;
    }
}
